//
//  JPWeatherView.m
//  主界面
//
//  Created by 刘刘 on 16/11/13.
//  Copyright © 2016年 刘刘. All rights reserved.
//

#import "JPWeatherView.h"
#import "UIColor+HMAddition.h"
#import <UIImageView+WebCache.h>

@interface JPWeatherView ()

/* 天气图片 */
@property (nonatomic, weak) UIImageView *weatherIV;
/* 天气文字 */
@property (nonatomic, weak) UILabel *weatherLb;
/* 城市 */
@property (nonatomic, weak) UILabel *cityLb;
/* 温度 */
@property (nonatomic, weak) UILabel *temperatureLb;
/* 日期 */
@property (nonatomic, weak) UILabel *dataLb;

@end

@implementation JPWeatherView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
    
}


/**
 加载天气数据
 */
- (void)setupUI {
    
    // 天气图片
    UIImageView *weatherIV = [[UIImageView alloc] init];
    [self addSubview:weatherIV];
    self.weatherIV = weatherIV;
    
    CGFloat margin = 16;
    [weatherIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(8);
        make.top.equalTo(self).offset(margin);
        make.bottom.equalTo(self).offset(-margin);
        make.width.equalTo(weatherIV.mas_height).multipliedBy(1.5f);
    }];
    [weatherIV setImage:[UIImage imageNamed:@"icon_weather_qing"]];
    
    // 分割线
    UIView *lineView = [[UIView alloc] init];
    [self addSubview:lineView];
    lineView.backgroundColor = [UIColor grayColor];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_equalTo(1);
        make.height.equalTo(weatherIV.mas_height);
    }];
    
    // 城市
    UILabel *cityLb = [[UILabel alloc] init];
    [self addSubview:cityLb];
    self.cityLb = cityLb;
    cityLb.text = @"深圳市";
    cityLb.font = [UIFont systemFontOfSize:15];
    cityLb.textColor = [UIColor hm_colorWithRed:64.0 green:198.0 blue:197.0];
    [cityLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineView.mas_left).offset(-16);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    // 天气状态
    UILabel *weatherLb = [[UILabel alloc] init];
    [self addSubview:weatherLb];
    self.weatherLb = weatherLb;
    weatherLb.text = @"晴";
    weatherLb.numberOfLines = 0;
    weatherLb.textAlignment = NSTextAlignmentCenter;
    weatherLb.font = [UIFont systemFontOfSize:12];
//    weatherLb.textColor = [UIColor hm_colorWithRed:64.0 green:198.0 blue:197.0];
    weatherLb.textColor = [UIColor darkGrayColor];
    [weatherLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cityLb.mas_left).offset(0);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(60);
    }];
    
    // 温度
    UILabel *temperatureLb = [[UILabel alloc] init];
    [self addSubview:temperatureLb];
    self.temperatureLb = temperatureLb;
    temperatureLb.text = @"温度: 20~26℃";
    temperatureLb.font = [UIFont systemFontOfSize:15];
    temperatureLb.textColor = [UIColor hm_colorWithRed:64.0 green:198.0 blue:197.0];
    [temperatureLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY).offset(-12);
        make.left.equalTo(lineView.mas_right).offset(25);
    }];
    
    // 日期
    UILabel *dataLb = [[UILabel alloc] init];
    [self addSubview:dataLb];
    self.dataLb = dataLb;
    dataLb.text = @"2016年11月11日";
    dataLb.font = [UIFont systemFontOfSize:15];
    dataLb.textColor = [UIColor hm_colorWithRed:64.0 green:198.0 blue:197.0];
    [dataLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineView.mas_right).offset(25);
        make.centerY.equalTo(self.mas_centerY).offset(12);
    }];
    
}


/**
 获取网络数据
 */
- (void)setWeatherModel:(JPWeatherModel *)weatherModel{
    
    _weatherModel = weatherModel;
    self.weatherLb.text = weatherModel.weather;
    self.cityLb.text = weatherModel.currentCity;
    self.temperatureLb.text = [NSString stringWithFormat:@"温度 : %@",weatherModel.temperature];
    self.dataLb.text = [NSString stringWithFormat:@"日期 : %@",weatherModel.date];
    [self.weatherIV sd_setImageWithURL:[NSURL URLWithString:weatherModel.dayPictureUrl] placeholderImage:[UIImage imageNamed:@"icon_weather_qing"]];
    }

@end
